package com.example.chenzhiran.myapplication7;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.chenzhiran.myapplication7.server.clientData;
import com.example.chenzhiran.myapplication7.server.serverData;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class PurchaseActivity extends AppCompatActivity {

    private EditText mAddressEditText;
    private Button mPayButton;

    clientData outdata5 = new clientData();
    public String productid = null;
    String username = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase);
        setTitle("购买页");

        mAddressEditText = (EditText) findViewById(R.id.address_editText);
        mPayButton = (Button)this.findViewById(R.id.pay_button);
        outdata5.pageId = 5;


        Intent intent = getIntent();
        if (intent != null) {
            productid = intent.getStringExtra("productid");
            username = intent.getStringExtra("username");
            outdata5.username = username;

        }

        mPayButton.setOnClickListener(mOnClickListener);

    }

    //点击事件
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()) {
                case R.id.pay_button:
                    String inputaddress = mAddressEditText.getText().toString();
                    if(hasWords(inputaddress) == true){
                        outdata5.inputmessage = inputaddress + " " + productid;
                        new PurchaseActivity.connection().execute(outdata5);
                    }

            }

        }
    };

    private boolean hasWords(String inputaddress)
    {
        if(inputaddress == null || inputaddress.length() == 0)
        {
            Toast.makeText(PurchaseActivity.this, "please input address",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }


    public class connection extends AsyncTask<clientData, Void, serverData> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected serverData doInBackground(clientData... params) {
            clientData data = params[0];
            serverData indata = new serverData();
            try {
                Socket socket = new Socket("103.242.1.105", 1080);
                ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                out.writeObject(data);
                out.flush();
                ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                indata = (serverData) in.readObject();
                in.close();
                out.close();
                socket.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return indata;
        }

        //返回的商品信息
        @Override
        protected void onPostExecute(serverData data) {
            super.onPostExecute(data);


            if(data.datastatus == 0){

                Toast.makeText( PurchaseActivity.this, "购买成功", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(PurchaseActivity.this, "购买失败", Toast.LENGTH_SHORT).show();
            }

        }
    }
}
